package cn.stylefeng.guns.modular.dms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.dms.entity.DirectoryFile;
import cn.stylefeng.guns.modular.dms.mapper.DirectoryFileMapper;
import cn.stylefeng.guns.modular.dms.model.params.DirectoryFileParam;
import cn.stylefeng.guns.modular.dms.model.result.DirectoryFileResult;
import  cn.stylefeng.guns.modular.dms.service.DirectoryFileService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-09
 */
@Service
public class DirectoryFileServiceImpl extends ServiceImpl<DirectoryFileMapper, DirectoryFile> implements DirectoryFileService {

    @Override
    public void add(DirectoryFileParam param){
        DirectoryFile entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public boolean delete(DirectoryFileParam param){
        return this.removeById(getKey(param));
    }

    @Override
    public boolean batchDelete(List ids) {
        return this.removeByIds(ids);
    }

    @Override
    public void update(DirectoryFileParam param){
        DirectoryFile oldEntity = getOldEntity(param);
        DirectoryFile newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DirectoryFileResult findBySpec(DirectoryFileParam param){
        return null;
    }

    @Override
    public List<DirectoryFileResult> findListBySpec(DirectoryFileParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DirectoryFileParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DirectoryFileParam param){
        return param.getFileId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DirectoryFile getOldEntity(DirectoryFileParam param) {
        return this.getById(getKey(param));
    }

    private DirectoryFile getEntity(DirectoryFileParam param) {
        DirectoryFile entity = new DirectoryFile();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
